/**
 * @see 该界面是“学生界面”的考勤记录详情
 */
import { timestampToTime } from '../../../../../utils/util'
Page({
  data: {
    courseData: [],
    currentItem: {}, // 从上一层传回来的数据
    appMsg: {
      type: '',
      show: false
    }
  },
  onLoad (option) {
    const { item } = option
    try {
      this.setData({ currentItem: JSON.parse(item) })
      this.getStudentAttendance()
    } catch (error) {
      wx.showToast({
        title: '数据异常，请稍后再试',
        icon: 'none',
        duration: 2000
      })
    }
  },
  /**
   * @see 接口--获取获取学生考勤详情
   */
  getStudentAttendance () {
    const { courseId } = this.data.currentItem
    const request = { courseId }
    this.setData({
      appMsg: {
        type: 'loading',
        show: true
      }
    })
    wx.$API['schoolStudy/getStudentAttendance'](request).then((result) => {
      const { data } = result
      data.forEach(item => {
        if (item.attendanceTime === 0 || !item.attendanceTime) {
          item.attendTimeText = '无'
        } else {
          item.attendTimeText = timestampToTime(item.attendanceTime)
        }
        switch (item.attendanceWay) {
          case 1:
            item.attendanceWayText = '定位签到'
            break
          case 2:
            item.attendanceWayText = '扫一扫签到'
            break
          case 1:
            item.attendanceWayText = '邀请码签到'
            break
          default:
            break
        }
        switch (item.attendanceStatus) {
          case 1:
            item.attendanceStatusText = '签到'
            item.class = 'status1'
            break
          case 2:
            item.attendanceStatusText = '位置异常'
            item.class = 'status2'
            break
          case 3:
            item.attendanceStatusText = '迟到'
            item.class = 'status2'
            break
          case 4:
            item.attendanceStatusText = '请假'
            item.class = 'status4'
            break
          case 5:
            item.attendanceStatusText = '缺勤'
            item.class = 'status3'
            break
          case 6:
            item.attendanceStatusText = '免听'
            item.class = 'status4'
            break
          default:
            break
        }
      })
      this.setData({
        courseData: data,
        appMsg: {
          type: 'loading',
          show: false
        }
      })
    }).catch((error) => {
      this.setData({
        appMsg: {
          type: 'err',
          show: true
        }
      })
    })
  }
})